package com.example.demo.controller;

import com.alibaba.fastjson2.JSONObject;
import com.example.demo.api.LoginApi;
import com.example.demo.dto.ApifoxModel;
import com.example.demo.dto.WeixinMsg;
import com.example.demo.service.LoginService;
import com.example.demo.service.MsgHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author gsq
 * @date 2025/3/10 18:00
 */
@Slf4j
@RestController
public class LoginController {

    @Resource
    private LoginService loginService;
    @Resource
    private MsgHandler msgHandler;

    @GetMapping("/login")
    public String login() {
        loginService.login();
        return "success";
    }

    @GetMapping("/logout")
    public JSONObject logout(String appId) {
        return LoginApi.logout(appId);
    }


    @GetMapping("/checkOnline")
    public JSONObject checkOnline(String appId) {
        return LoginApi.checkOnline(appId);
    }

    @GetMapping("/deviceList")
    public JSONObject deviceList() {
        return LoginApi.deviceList();
    }

    @RequestMapping("/callback")
    public ApifoxModel<Void> callback(@RequestBody WeixinMsg weixinMsg) {
        log.info("收到回调消息：{}", weixinMsg);
        msgHandler.handler(weixinMsg);

        ApifoxModel<Void> apifoxModel = new ApifoxModel<>();
        apifoxModel.setRet(200L);
        apifoxModel.setMsg("操作成功");
        return apifoxModel;
    }
}
